<?php

namespace App\Models\Anchor;

use App\Models\BaseModel;

class AnchorWorksModel extends BaseModel
{
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'anchor_works';
    /**
     * 与表关联的主键
     *
     * @var string
     */
    protected $primaryKey = 'id';
    /**
     * 执行模型是否自动维护时间戳。
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = array(
        "anchor_id",
        "author_id",
        "status",
        "created_at",
        "over_at",
        "sales",
        "anchor_type_id",
        "title",
        "cost"
    );

    /**
     * 查询数据需要过滤掉的属性。
     *
     * @var array
     */

    protected $hidden = array(
        'updated_at'
    );

//获取达人订单在直播里下的单的直播场次数据
    public function getOrdersAuthorInAnchorWorks(&$worksRows, $author_id, $order_created_at)
    {
        $ordersTime = strtotime($order_created_at);
        //订单下单时间要在直播时间内才算
        $result = [];
        foreach ($worksRows as $k => $v) {
            $workStartTime = strtotime($v['created_at']);
            $workOverTime = empty($v['over_at']) ? time() : strtotime($v['over_at']);
            if ($v['author_id'] == $author_id && ($ordersTime >= $workStartTime && $ordersTime <= $workOverTime)) {
                $result = $v;
                break;
            }
        }
        return $result;
    }

//    一对一关联达人
    public function author()
    {
        return $this->hasOne('App\Models\Anchor\AnchorAuthorModel', 'author_id', 'author_id');
    }

//    主播
    public function anchor()
    {
        return $this->hasOne('App\Models\Anchor\AnchorModel', 'id', 'anchor_id');
    }

    //    一对一关联类型
    public function type()
    {
        return $this->hasOne('App\Models\Anchor\AnchorTypeModel', 'id', 'anchor_type_id');
    }
}
